<?php
/**
 * Pages Widget
 *
 * Replaces the default WordPress Pages widget.
 * @link http://themehybrid.com/themes/hybrid/widgets
 *
 * In 0.6, converted functions to a class that extends WP 2.8's widget class.
 *
 * @package Hybrid
 * @subpackage Widgets
 */

/**
 * Output of the Pages widget.
 * Arguments are parameters of the wp_list_pages() function.
 * @link http://codex.wordpress.org/Template_Tags/wp_list_pages
 *
 * @since 0.6
 */
class Hybrid_Widget_Pages extends WP_Widget {

	var $prefix;
	var $textdomain;

	function Hybrid_Widget_Pages() {
		$this->prefix = hybrid_get_prefix();
		$this->textdomain = hybrid_get_textdomain();

		$widget_ops = array( 'classname' => 'pages', 'description' => __( 'An advanced widget that gives you total control over the output of your page links.', $this->textdomain) );
		$control_ops = array( 'width' => 800, 'height' => 350, 'id_base' => "{$this->prefix}-pages" );
		$this->WP_Widget( "{$this->prefix}-pages", __( 'Pages', $this->textdomain), $widget_ops, $control_ops );
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title = apply_filters( 'widget_title', $instance['title'] );
		$sort_column = $instance['sort_column'];
		$sort_order = $instance['sort_order'];
		$exclude = $instance['exclude'];
		$include = $instance['include'];
		$depth = (int)$instance['depth'];
		$child_of = $instance['child_of'];
		$meta_key = $instance['meta_key'];
		$meta_value = $instance['meta_value'];
		$authors = $instance['authors'];
		$exclude_tree = $instance['exclude_tree'];
		$link_before = $instance['link_before'];
		$link_after = $instance['link_after'];
		$date_format = $instance['date_format'];
		$show_date = $instance['show_date'];
		$number = (int)$instance['number'];
		$offset = (int)$instance['offset'];

		$hierarchical = isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : false;
		$show_home = isset( $instance['show_home'] ) ? $instance['show_home'] : false;

		if ( !$date_format )
			$date_format = get_option( 'date_format' );

		$pages = array(
			'depth' => $depth,
			'sort_column' => $sort_column,
			'sort_order' => $sort_order,
			'show_date' => $show_date,
			'date_format' => $date_format,
			'child_of' => $child_of,
			'exclude' => $exclude,
			'include' => $include,
			'hierarchical' => 0,//$hierarchical,
			'meta_key' => $meta_key,
			'meta_value' => $meta_value,
			'authors' => $authors,
			'exclude_tree' => $exclude_tree,
			'link_before' => $link_before,
			'link_after' => $link_after,
			'number' => $number,
			'offset' => $offset,
			'title_li' => '',
			'echo' => 0,
		);

		echo $before_widget;

		if ( $title )
			echo $before_title . $title . $after_title;

		echo '<ul class="xoxo pages">';

		if ( $show_home )
			echo '<li class="page_item' . ( ( is_home() ) ? ' current_page_item' : '' ) . '"><a href="' . get_bloginfo( 'url' ) . '" title="' . get_bloginfo( 'name' ) . '">' . $link_before . __( 'Home', $this->textdomain) . $link_after . '</a></li>';

		echo str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages( $pages ) );

		echo '</ul><!-- .xoxo .pages -->' . $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['depth'] = strip_tags( $new_instance['depth'] );
		$instance['child_of'] = strip_tags( $new_instance['child_of'] );
		$instance['exclude'] = strip_tags( $new_instance['exclude'] );
		$instance['include'] = strip_tags( $new_instance['include'] );
		$instance['meta_key'] = strip_tags( $new_instance['meta_key'] );
		$instance['meta_value'] = strip_tags( $new_instance['meta_value'] );
		$instance['authors'] = strip_tags( $new_instance['authors'] );
		$instance['exclude_tree'] = strip_tags( $new_instance['exclude_tree'] );
		$instance['date_format'] = strip_tags( $new_instance['date_format'] );
		$instance['number'] = strip_tags( $new_instance['number'] );
		$instance['offset'] = strip_tags( $new_instance['offset'] );
		$instance['sort_column'] = $new_instance['sort_column'];
		$instance['sort_order'] = $new_instance['sort_order'];
		$instance['show_date'] = $new_instance['show_date'];
		$instance['link_before'] = $new_instance['link_before'];
		$instance['link_after'] = $new_instance['link_after'];

		$instance['hierarchical'] = ( isset( $new_instance['hierarchical'] ) ? 1 : 0 );
		$instance['show_home'] = ( isset( $new_instance['show_home'] ) ? 1 : 0 );

		return $instance;
	}

	function form( $instance ) {

		//Defaults
		$defaults = array( 'title' => __( 'Pages', $this->textdomain), 'hierarchical' => true, 'sort_column' => 'post_title', 'sort_order' => 'ASC', 'date_format' => get_option( 'date_format' ) );
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<div style="float:left;width:31%;">
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:',$this->textdomain); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $instance['title']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'sort_column' ); ?>"><?php _e( 'Order By:',$this->textdomain); ?> <code>sort_column</code></label>
			<select id="<?php echo $this->get_field_id( 'sort_column' ); ?>" name="<?php echo $this->get_field_name( 'sort_column' ); ?>" class="widefat" style="width:100%;">
				<option <?php if ( 'post_title' == $instance['sort_column'] ) echo 'selected="selected"'; ?>>post_title</option>
				<option <?php if ( 'menu_order' == $instance['sort_column'] ) echo 'selected="selected"'; ?>>menu_order</option>
				<option <?php if ( 'post_date' == $instance['sort_column'] ) echo 'selected="selected"'; ?>>post_date</option>
				<option <?php if ( 'post_modified' == $instance['sort_column'] ) echo 'selected="selected"'; ?>>post_modified</option>
				<option <?php if ( 'ID' == $instance['sort_column'] ) echo 'selected="selected"'; ?>>ID</option>
				<option <?php if ( 'post_author' == $instance['sort_column'] ) echo 'selected="selected"'; ?>>post_author</option>
				<option <?php if ( 'post_name' == $instance['sort_column'] ) echo 'selected="selected"'; ?>>post_name</option>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'sort_order' ); ?>"><?php _e( 'Sort Order:', $this->textdomain); ?> <code>sort_order</code></label>
			<select id="<?php echo $this->get_field_id( 'sort_order' ); ?>" name="<?php echo $this->get_field_name( 'sort_order' ); ?>" class="widefat" style="width:100%;">
				<option <?php if ( 'ASC' == $instance['sort_order'] ) echo 'selected="selected"'; ?>>ASC</option>
				<option <?php if ( 'DESC' == $instance['sort_order'] ) echo 'selected="selected"'; ?>>DESC</option>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'depth' ); ?>"><?php _e( 'Depth:', $this->textdomain); ?> <code>depth</code></label>
			<input id="<?php echo $this->get_field_id( 'depth' ); ?>" name="<?php echo $this->get_field_name( 'depth' ); ?>" type="text" value="<?php echo $instance['depth']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number:', $this->textdomain); ?> <code>number</code></label>
			<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $instance['number']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'child_of' ); ?>"><?php _e( 'Child Of:', $this->textdomain); ?> <code>child_of</code></label>
			<input id="<?php echo $this->get_field_id( 'child_of' ); ?>" name="<?php echo $this->get_field_name( 'child_of' ); ?>" type="text" value="<?php echo $instance['child_of']; ?>" style="width:100%;" />
		</p>
		</div>

		<div style="float:left;width:31%;margin-left:3.5%;">
		<p>
			<label for="<?php echo $this->get_field_id( 'include' ); ?>"><?php _e( 'Include:', $this->textdomain); ?> <code>include</code></label>
			<input id="<?php echo $this->get_field_id( 'include' ); ?>" name="<?php echo $this->get_field_name( 'include' ); ?>" type="text" value="<?php echo $instance['include']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><?php _e( 'Exclude:', $this->textdomain); ?> <code>exclude</code></label>
			<input id="<?php echo $this->get_field_id( 'exclude' ); ?>" name="<?php echo $this->get_field_name( 'exclude' ); ?>" type="text" value="<?php echo $instance['exclude']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'exclude_tree' ); ?>"><?php _e( 'Exclude Tree:', $this->textdomain); ?> <code>exclude_tree</code></label>
			<input id="<?php echo $this->get_field_id( 'exclude_tree' ); ?>" name="<?php echo $this->get_field_name( 'exclude_tree' ); ?>" type="text" value="<?php echo $instance['exclude_tree']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'offset' ); ?>"><?php _e( 'Offset:', $this->textdomain); ?> <code>offset</code></label>
			<input id="<?php echo $this->get_field_id( 'offset' ); ?>" name="<?php echo $this->get_field_name( 'offset' ); ?>" type="text" value="<?php echo $instance['offset']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'meta_key' ); ?>"><?php _e( 'Meta Key:', $this->textdomain); ?> <code>meta_key</code></label>
			<input id="<?php echo $this->get_field_id( 'meta_key' ); ?>" name="<?php echo $this->get_field_name( 'meta_key' ); ?>" type="text" value="<?php echo $instance['meta_key']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'meta_value' ); ?>"><?php _e( 'Meta Value:', $this->textdomain); ?> <code>meta_value</code></label>
			<input id="<?php echo $this->get_field_id( 'meta_value' ); ?>" name="<?php echo $this->get_field_name( 'meta_value' ); ?>" type="text" value="<?php echo $instance['meta_value']; ?>" style="width:100%;" />
		</p>
		</div>

		<div style="float:right;width:31%;margin-left:3.5%;">
		<p>
			<label for="<?php echo $this->get_field_id( 'authors' ); ?>"><?php _e( 'Authors:', $this->textdomain); ?> <code>authors</code></label>
			<input id="<?php echo $this->get_field_id( 'authors' ); ?>" name="<?php echo $this->get_field_name( 'authors' ); ?>" type="text" value="<?php echo $instance['authors']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'link_before' ); ?>"><?php _e( 'Before Link:', $this->textdomain); ?> <code>link_before</code></label>
			<input id="<?php echo $this->get_field_id( 'link_before' ); ?>" name="<?php echo $this->get_field_name( 'link_before' ); ?>" type="text" value="<?php echo $instance['link_before']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'link_after' ); ?>"><?php _e( 'After Link:', $this->textdomain); ?> <code>link_after</code></label>
			<input id="<?php echo $this->get_field_id( 'link_after' ); ?>" name="<?php echo $this->get_field_name( 'link_after' ); ?>" type="text" value="<?php echo $instance['link_after']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Show date?:', $this->textdomain); ?> <code>show_date</code></label>
			<select id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" class="widefat" style="width:100%;">
				<option <?php if ( '' == $instance['show_date'] ) echo 'selected="selected"'; ?>></option>
				<option <?php if ( 'created' == $instance['show_date'] ) echo 'selected="selected"'; ?>>created</option>
				<option <?php if ( 'modified' == $instance['show_date'] ) echo 'selected="selected"'; ?>>modified</option>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'date_format' ); ?>"><?php _e( 'Date Format:', $this->textdomain); ?> <code>date_format</code></label>
			<input id="<?php echo $this->get_field_id( 'date_format' ); ?>" name="<?php echo $this->get_field_name( 'date_format' ); ?>" type="text" value="<?php echo $instance['date_format']; ?>" style="width:100%;" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>">
			<input class="checkbox" type="checkbox" <?php checked( $instance['hierarchical'], true ); ?> id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>" /> <?php _e( 'Hierarchical?', $this->textdomain); ?> <code>hierarchical</code></label>
		</p>
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_home'], true ); ?> id="<?php echo $this->get_field_id( 'show_home' ); ?>" name="<?php echo $this->get_field_name( 'show_home' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'show_home' ); ?>"><?php _e( 'Show home?', $this->textdomain); ?> <code>show_home</code></label>
		</p>
		</div>
		<div style="clear:both;">&nbsp;</div>
	<?php
	}
}

?>